﻿using HY.Common;
using System;
using System.Windows;
using System.Windows.Input;
using WpfApp.ViewModels;

namespace WpfApp.Views
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    public partial class LoginView : Window
    {
        LoginViewModel vm = new LoginViewModel();
        public LoginView()
        {
            InitializeComponent();
            this.DataContext = vm;
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLogin_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SubmitLogin();
        }

        private void BtnClose_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DialogResult = false;
        }

        private void PwdBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SubmitLogin();
            }
        }

        private void SubmitLogin()
        {
            try
            {
                if(vm.UserName=="admin" && vm.Password == "123456")
                {
                    this.DialogResult = true;
                }
                else
                {
                    vm.Message = "用户名或密码不正确";
                }
            }
            catch (Exception ex)
            {
                vm.Message = "登录失败，请检查网络";
                LogHelper.Error(nameof(SubmitLogin), ex);
            }
        }
    }
}
